package com.neusoft.neuiotms.sap.fixture;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.auth.model.AuthOganization;
import com.neusoft.bizcore.auth.model.AuthRole;
import com.neusoft.bizcore.auth.repository.AuthOganizationRepository;
import com.neusoft.bizcore.auth.repository.AuthRoleRepository;

@Component
public class HnInitialDataFixture implements CommandLineRunner {

    @Autowired
    private AuthOganizationRepository authOganizationRepository;
    @Autowired
    private AuthRoleRepository authRoleRepository;

    @Override
    public void run(final String... args) throws Exception {
        this.init();
    }

    private void init() {
        AuthOganization haishuOrg = this.authOganizationRepository.findByCode("haishu");
        if (null == haishuOrg) {
            haishuOrg = new AuthOganization();
            haishuOrg.setCode("haishu");
            haishuOrg.setName("海数");
            this.authOganizationRepository.save(haishuOrg);
        }

        AuthOganization dianxinjichengOrg = this.authOganizationRepository.findByCode("dianxinjicheng");
        if (null == dianxinjichengOrg) {
            dianxinjichengOrg = new AuthOganization();
            dianxinjichengOrg.setCode("dianxinjicheng");
            dianxinjichengOrg.setName("电信集成");
            this.authOganizationRepository.save(dianxinjichengOrg);
        }

        // 电信集成公司的角色
        final String[] workerRoles =
                new String[] {"workerGroupLeader,一线组长", "worker,一线工程师", "expertGroupLeader,二线组长", "expert,二线工程师",
                        "backup,呼叫中心" };

        for (final String codename : workerRoles) {
            final String code = codename.split(",")[0];
            final String name = codename.split(",")[1];
            AuthRole role = this.authRoleRepository.findByCode(code);
            if (null == role) {
                role = new AuthRole();
                role.setCode(code);
                role.setName(name);
                this.authRoleRepository.save(role);
            }
        }

        // 海数公司的角色
        final String[] leaderRoles =
                new String[] {"zongjian,项目主管" };

        for (final String codename : leaderRoles) {
            final String code = codename.split(",")[0];
            final String name = codename.split(",")[1];
            AuthRole role = this.authRoleRepository.findByCode(code);
            if (null == role) {
                role = new AuthRole();
                role.setCode(code);
                role.setName(name);
                this.authRoleRepository.save(role);
            }
        }

    }

}
